/*Copyright 2010 by Matt Van Der Westhuizen <mattpwest@gmail.com> */

package org.chaoticengine.ast.view;

import org.chaoticengine.ast.GlobalData;
import org.chaoticengine.ast.model.World;
import org.chaoticengine.ast.pattern.ViewController;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

/**
 * The TimeView displays the current date and time, as well as acting as a
 * clickable control to end the current turn.
 * 
 * @author Matt Van Der Westhuizen <mattpwest@gmail.com>
 */
public class TimeView extends ViewController<World> {
    protected World world = null;
    protected int time = 0;

    @Override
    public World getModel() {
        return(world);
    }

    @Override
    public void keyPressed(int key, char c) {
        if (key == Input.KEY_SPACE) {
            World wrld = GlobalData.getInstance().getWorld();
            wrld.togglePaused();
        }
    }

    @Override
    public void keyReleased(int key, char c) {}

    @Override
    public void onDeselect() {}

    @Override
    public void onMouseEnter() {
        this.hasFocus = true;
    }

    @Override
    public void onMouseExit() {
        this.hasFocus = false;
    }

    @Override
    public void onMouseMove(int oldx, int oldy, int newx, int newy) {}

    @Override
    public void onMouseClick(int x, int y, int button) {
    }

    @Override
    public void onMouseRelease(int x, int y, int button) {
        if (!hasFocus) return;
        
        world.takeTurn(world.getTurnLength());
    }

    @Override
    public void onMouseWheelDown() {}

    @Override
    public void onMouseWheelUp() {}

    @Override
    public void onSelect() {}

    @Override
    public void refresh(World model) {
        this.world = model;
    }

    @Override
    public void render(GameContainer gc, StateBasedGame sbg, Graphics grphcs) throws SlickException {
        grphcs.setColor(Color.white);
        grphcs.drawString("Day: " + world.getDay() + " - Hour: " + world.getHours(),
                this.getPosition().x, this.getPosition().y);
    }

    @Override
    public void update(GameContainer gc, StateBasedGame sbg, int delta) {
        if (!world.getPaused()) {
            time += delta;

            if (time > 200) {
                time -= 200;

                world.takeTurn(world.getTurnLength());
            }
        }
    }
}
